package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Amos
 * @date 2021/10/11
 */

@RestController
@CrossOrigin
@RequestMapping("/axios")
public class AxiosController {

    @Autowired
    private UserService userService;

    /**
     * URL:http://localhost:8080/axios/getUserById?id=1
     * 参数：id=1
     * 返回值：User对象
     */
    @GetMapping("/getUserById")
    public User getUserById(Integer id) {
        return userService.findById(id);
    }

    /**
     * GET测试2：restFul结构接收参数
     * URL:http://localhost:8080/axios/user/女/18
     * 参数：sex/age
     * 返回值: List集合
     */
    @GetMapping("/user/{sex}/{age}")
    public List<User> getUserBySA(User user) {
        return userService.getUserBySA(user);
    }

    /**
     * GET测试3：利用对象的方式接收参数
     * URL：http://localhost:8080/axios/getUser?name=xx&sex=xx&age=xx
     * 参数: name/age/sex
     * 返回值： List<User>
     */
    @GetMapping("/getUser")
    public List<User> getUser(User user) {
        return userService.getUser(user);
    }

    /**
     * DELETE测试1：axios前后端调用，根据id删除数据
     * url: http://localhost:8080/axios/deleteById?id=321
     * @param id
     */
    @DeleteMapping("/deleteById")
    public String deleteById(Integer id) {
        userService.deleteById(id);
        return "删除数据成功！！！";
    }

}
